//
//  indexView.m
//  Estate
//
//  Created by heyong on 16/5/25.
//  Copyright © 2016年 ttsx. All rights reserved.
//


#import "indexView.h"
#import "DataSynchronization.h"

@implementation indexView
- (UILabel *)labFlag{
    if(_labFlag==nil){
        _labFlag=[[UILabel alloc]init];
    }
    return _labFlag;
}
- (UILabel *)btnTitle{
    if(_btnTitle==nil){
        _btnTitle=[[UILabel alloc]init];
    }
    return _btnTitle;
}
- (UIButton *)qieHuanBtn{
    if(_qieHuanBtn==nil){
        _qieHuanBtn=[[UIButton alloc]init];
        _qieHuanBtn.tag=100;
    }
    return _qieHuanBtn;
}
- (UIButton *)qianDaoBtn{
    if(_qianDaoBtn==nil){
        _qianDaoBtn=[[UIButton alloc]init];
        _qianDaoBtn.tag=101;
    }
    return _qianDaoBtn;
}

-(UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]init];
    }
    return  _tableView;
}

- (UILabel *)photoNum{
    if(_photoNum==nil){
        _photoNum=[[UILabel alloc]init];
    }
    return _photoNum;
}

//同步按钮
-(UIButton *)tongbuBtn{
    if(_tongbuBtn==nil){
        _tongbuBtn=[[UIButton alloc]init];
    }
    return _tongbuBtn;
}

- (UIImageView *)iconImage{
    if(!_iconImage){
        _iconImage=[[UIImageView alloc]init];
    }
    return _iconImage;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self==[super initWithFrame:frame]){
        //创建视图
        [self createTop];
        [self createBtnView];
        [self createRecord];
    }
    return self;
}

-(void)setIshidden:(BOOL)ishidden{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.iconImage.hidden=ishidden;
    });
    
}

-(void)setIconType:(NSString *)iconType{
    _iconType=iconType;
    //上传
    if([_iconType isEqualToString:@"sc"])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.iconImage.image=[UIImage imageNamed:@"sync_shangchuan"];
        });
    }
    //下载
    if([_iconType isEqualToString:@"xz"]){
        dispatch_async(dispatch_get_main_queue(), ^{
            self.iconImage.image=[UIImage imageNamed:@"sync_xiazai"];
        });
        
    }
    //上传和下载
    if([_iconType isEqualToString:@"scxz"]){
        dispatch_async(dispatch_get_main_queue(), ^{
            self.iconImage.image=[UIImage imageNamed:@"sync_shangchuanxiazai"];
        });
    }
}

//!!!!:顶部视图
- (void)createTop
{
    UIView *view=[[UIView alloc]initWithFrame:CGRECTS(0,0, 375, 50)];
    view.backgroundColor=[UIColor whiteColor];
    view.layer.borderWidth=0.5;
    view.layer.borderColor=[UIColor colorWithRed:0.87 green:0.88 blue:0.88 alpha:1.00].CGColor;
    
    _ProjectTitle=[[UILabel alloc]init];
    _ProjectTitle.textColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    _ProjectTitle.font=[UIFont systemFontOfSize:16.*BIZ];
    
    [view addSubview:_ProjectTitle];
    
    _ProjectTitle.frame=CGRECTS(16, (50-32)/2, 250, 32);
    
    //离岗在岗
    self.labFlag.layer.cornerRadius=10.;
    self.labFlag.clipsToBounds = YES;
    self.labFlag.textAlignment=1;
    self.labFlag.font=[UIFont systemFontOfSize:14*BIZ];
    self.labFlag.textColor=[UIColor whiteColor];
    [view addSubview:self.labFlag];
    [self.labFlag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(BIH(25));
        make.width.offset(BIW(60));
        make.centerY.mas_equalTo(_ProjectTitle.mas_centerY);
        make.right.offset(-16);
    }];
    
    [self addSubview:view];
}

//!!!!:按钮视图
- (void)createBtnView
{
    view1=[[UIView alloc]initWithFrame:CGRECTS(0, 60, 375, 105)];
    view1.backgroundColor=[UIColor whiteColor];
    view1.layer.borderWidth=0.5;
    view1.layer.borderColor=[UIColor colorWithRed:0.87 green:0.88 blue:0.88 alpha:1.00].CGColor;
    [self addSubview:view1];
    
    //项目切换
    self.qieHuanBtn.backgroundColor=[UIColor colorWithRed:0.49 green:0.75 blue:0.16 alpha:1.00];
    self.qieHuanBtn.layer.cornerRadius=20.;
    [self.qieHuanBtn setImage:[UIImage imageNamed:@"xmqh"] forState:UIControlStateNormal];
    [self.qieHuanBtn setAdjustsImageWhenHighlighted:NO];
    [view1 addSubview: self.qieHuanBtn];
    [_qieHuanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(DEF_SCREEN_WIDTH/15);
        make.width.mas_offset(DEF_SCREEN_WIDTH/6);
        make.height.mas_offset(DEF_SCREEN_WIDTH/6);
        make.top.offset(12*BIZ);
    }];
    [self createLab:self.qieHuanBtn AndTitle:@"项目切换"];
    /*
     *  甲方  巡检员  巡检组长，没有签到，同步放中间。
     */
    NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
    
    //巡检员也加上签到签退
    if([USERTYPE.typeArray containsObject:role]||[USERTYPE.typeArrayNoWeather containsObject:role]){
        [self wuQianDao];
    }else{
        [self qiandao];
        
    }
    
    //同步感叹号
    [view1 addSubview:self.iconImage];
    self.iconImage.image=[UIImage imageNamed:@"tishi"];
    self.iconImage.hidden=YES;
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.tongbuBtn.mas_top).offset(15.);
        make.right.mas_equalTo(self.tongbuBtn.mas_right);
        make.width.mas_offset(20);
        make.height.mas_offset(20);
    }];
    
    
    //本地图片
    self.photoBtn=[[UIButton alloc]init];
    self.photoBtn.backgroundColor=[UIColor colorWithRed:0.27 green:0.69 blue:0.92 alpha:1.00];
    self.photoBtn.layer.cornerRadius=20.;
    [self.photoBtn setImage:[UIImage imageNamed:@"tupian"] forState:UIControlStateNormal];
    [self.photoBtn setAdjustsImageWhenHighlighted:NO];
    [view1 addSubview: self.photoBtn];
    [_photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-DEF_SCREEN_WIDTH/15);
        make.width.mas_offset(DEF_SCREEN_WIDTH/6);
        make.height.mas_offset(DEF_SCREEN_WIDTH/6);
        make.top.offset(12*BIZ);
    }];
    [self createLab:self.photoBtn AndTitle:@"本地图片"];
    
    //图片文字
    [view1 addSubview:self.photoNum];
    self.photoNum.backgroundColor=[UIColor redColor];
    self.photoNum.font=[UIFont systemFontOfSize:10.];
    self.photoNum.layer.cornerRadius=10.;
    self.photoNum.layer.masksToBounds=YES;
    self.photoNum.textColor=[UIColor whiteColor];
    self.photoNum.textAlignment=1;
    [self.photoNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.photoBtn.mas_top).offset(15.);
        make.right.mas_equalTo(self.photoBtn.mas_right);
        make.width.mas_offset(20);
        make.height.mas_offset(20);
    }];
}
-(void)wuQianDao{
    //同步按钮
    self.tongbuBtn.backgroundColor=UIColorFromRGB(0xffa11a);
    self.tongbuBtn.layer.cornerRadius=20.;
    [self.tongbuBtn setImage:[UIImage imageNamed:@"tongbu"] forState:UIControlStateNormal];
    [self.tongbuBtn setAdjustsImageWhenHighlighted:NO];
    [view1 addSubview: self.tongbuBtn];
    [_tongbuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(DEF_SCREEN_WIDTH/6);
        make.centerX.mas_equalTo(view1.mas_centerX);
        make.height.mas_offset(DEF_SCREEN_WIDTH/6);
        make.top.offset(12*BIZ);
    }];
    [self createLab:self.tongbuBtn AndTitle:@"同步"];
}
-(void)qiandao{
    WEAKSELF
    
    //同步按钮
    self.tongbuBtn.backgroundColor=UIColorFromRGB(0xffa11a);
    self.tongbuBtn.layer.cornerRadius=20.;
    [self.tongbuBtn setImage:[UIImage imageNamed:@"tongbu"] forState:UIControlStateNormal];
    [self.tongbuBtn setAdjustsImageWhenHighlighted:NO];
    [view1 addSubview: self.tongbuBtn];
    [_tongbuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(DEF_SCREEN_WIDTH/6);
        make.left.mas_equalTo(weakSelf.qieHuanBtn.mas_right).offset(DEF_SCREEN_WIDTH/15);
        make.height.mas_offset(DEF_SCREEN_WIDTH/6);
        make.top.offset(12*BIZ);
    }];
    [self createLab:self.tongbuBtn AndTitle:@"同步"];
    
    //签到
    self.qianDaoBtn.backgroundColor=[UIColor colorWithRed:0.98 green:0.40 blue:0.40 alpha:1.00];
    self.qianDaoBtn.layer.cornerRadius=20.;
    [self.qianDaoBtn setImage:[UIImage imageNamed:@"qiandao"] forState:UIControlStateNormal];
    [self.qianDaoBtn setImage:[UIImage imageNamed:@"qiandao"] forState:UIControlStateHighlighted];
    [view1 addSubview: self.qianDaoBtn];
    [_qianDaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(DEF_SCREEN_WIDTH/6);
        make.height.mas_offset(DEF_SCREEN_WIDTH/6);
        make.left.mas_equalTo(weakSelf.tongbuBtn.mas_right).offset(DEF_SCREEN_WIDTH/15);
        make.top.offset(12*BIZ);
    }];
    
    [self createLab:self.qianDaoBtn AndTitle:@"签到"];
}

//创建按钮下文字
- (void)createLab:(UIButton *)btn AndTitle:(NSString *)title
{
    if(btn.tag==101){
        self.btnTitle.text=title;
        self.btnTitle.textColor=[UIColor colorWithRed:0.40 green:0.40 blue:0.40 alpha:1.00];
        self.btnTitle.font=[UIFont systemFontOfSize:15.*BIZ];
        self.btnTitle.textAlignment=1;
        [view1 addSubview:  self.btnTitle];
        [self.btnTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(btn.mas_bottom).offset(4*BIZ);
            make.width.offset(100*BIZ);
            make.height.offset(20*BIZ);
            make.centerX.mas_equalTo(btn.mas_centerX);
        }];
        
    }else{
        UILabel *lab1=[[UILabel alloc]init];
        lab1.text=title;
        lab1.textColor=[UIColor colorWithRed:0.40 green:0.40 blue:0.40 alpha:1.00];
        lab1.font=[UIFont systemFontOfSize:15.*BIZ];
        lab1.textAlignment=1;
        [view1 addSubview:lab1];
        [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(btn.mas_bottom).offset(4*BIZ);
            make.width.offset(100*BIZ);
            make.height.offset(20*BIZ);
            make.centerX.mas_equalTo(btn.mas_centerX);
        }];
    }
}


//!!!!:记录表格
- (void)createRecord
{
    //记录文字
    UILabel *recoreLab=[[UILabel alloc]initWithFrame:CGRECTS(0, 285, 375, 40)];
    recoreLab.layer.borderWidth=0.5;
    recoreLab.layer.borderColor=[UIColor colorWithRed:0.87 green:0.88 blue:0.88 alpha:1.00].CGColor;
    recoreLab.backgroundColor=[UIColor whiteColor];
    recoreLab.textAlignment=1;
    recoreLab.textColor=[UIColor colorWithRed:0.10 green:0.10 blue:0.10 alpha:1.00];
    recoreLab.font=[UIFont systemFontOfSize:18*BIZ];
    
    NSString *role = DEF_PERSISTENT_GET_OBJECT(@"role");
    if ([role isEqualToString:@"USER_01"] || [role isEqualToString:@"USER_03"]||[role isEqualToString:@"USER_07"]) {
        recoreLab.text = @"待处理事项";
        
    }else{
        recoreLab.text = @"签到记录";
        
    }
    
    //    recoreLab.textColor=UIColorFromRGB(0x787878);
    [self addSubview:recoreLab];
    
    /*
     *  甲方  巡检员  巡检组长 显示当前天气情况。
     */
    if([USERTYPE.typeArray containsObject:role]){
        recoreLab.text=@"今日天气";
    }else if ([USERTYPE.typeArrayNoWeather containsObject:role]){
        recoreLab.text=@"";
    } else if([role isEqualToString:@"1"] || [role isEqualToString:@"2"]) {
        recoreLab.text = @"";
        recoreLab.frame = CGRECTS(0, 285, 375, 0);
    }
    
    //设置表格
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(recoreLab.mas_bottom).offset(10*BIZ);
        make.left.offset(0).with.right.offset(0);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-112);
    }];
    
    self.tableView.layer.borderWidth=0.5;
    self.tableView.layer.borderColor=[UIColor colorWithRed:0.87 green:0.88 blue:0.88 alpha:1.00].CGColor;;
    self.tableView.showsVerticalScrollIndicator=NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"indexTableCell" bundle:nil] forCellReuseIdentifier:@"index"];
    //隐藏多与线条
    [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
}

- (void)setState:(int)state{
    if(state==0){
        self.btnTitle.text=@"签到";
    }else{
        self.btnTitle.text=@"签退";
    }
}

-(void)setdata{
    if(self.state==0){
        [self createLab:self.qianDaoBtn AndTitle:@"签到"];
        self.labFlag.text=@"离岗";
        self.labFlag.backgroundColor=[UIColor colorWithRed:0.58 green:0.58 blue:0.58 alpha:1.00];
    }else if(self.state==1){
        [self createLab:self.qianDaoBtn AndTitle:@"签退"];
        self.labFlag.text=@"在岗";
        self.labFlag.backgroundColor=[UIColor colorWithRed:0.00 green:0.58 blue:0.53 alpha:1.00];
    }
}

-(void)setProModel:(ProjectModel *)proModel{
    /*
     *  甲方  巡检员  巡检组长 显示智慧监理
     */
    NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    if([USERTYPE.typeArray containsObject:role]||[USERTYPE.typeArrayNoWeather containsObject:role]){
        _ProjectTitle.text=[infoDictionary objectForKey:@"CFBundleDisplayName"];;
        _labFlag.text = @"在岗";
        _labFlag.backgroundColor=[UIColor colorWithRed:0.00 green:0.58 blue:0.53 alpha:1.00];
    }else if([role isEqualToString:@"1"] || [role isEqualToString:@"2"]){
        //设置项目名
        _ProjectTitle.text=[infoDictionary objectForKey:@"CFBundleDisplayName"];;
    }else{
        //设置项目名
        _proModel=proModel;
        _ProjectTitle.text=_proModel.projectName;
    }
    
}
- (void)setAdvArr:(NSMutableArray *)advArr{
    
    _advArr=advArr;
    
    _cycleScrollView2=nil;
    self.controller.automaticallyAdjustsScrollViewInsets=NO;
    
    float x=10*BIZ;
    float y=175*BIZ;
    _cycleScrollView2=[[SDCycleScrollView alloc]initWithFrame:CGRectMake(x, y, DEF_SCREEN_WIDTH-20, 100*BIZ)];
    _cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    // 自定义分页控件小圆标颜色
    _cycleScrollView2.currentPageDotColor = [UIColor whiteColor];
    _cycleScrollView2.pageDotColor=[[UIColor whiteColor]colorWithAlphaComponent:0.65];
    //设置秒数
    _cycleScrollView2.autoScrollTimeInterval=2;
    // 添加
    [self addSubview:_cycleScrollView2];
    
    //图片填充方式
    // _cycleScrollView2.bannerImageViewContentMode=UIViewContentModeCenter;
    //网络请求失败加载默认图片
    _cycleScrollView2.placeholderImage=[UIImage imageNamed:@"banner_default"];
    //  _cycleScrollView2.placeholderImage=[UIImage imageNamed:@"banner"];
    
    if(_advArr.count>0){
        
        _cycleScrollView2.imageURLStringsGroup = _advArr;
        
    }else{
        NSMutableArray *arr=[NSMutableArray array];
        [arr addObject:[UIImage imageNamed:@"banner_default"]];
        _cycleScrollView2.localizationImageNamesGroup = arr;
    }
    
}




@end


